home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python152_Src.lha / Python152_Source / Amiga / envvars.c < prev    next >
C/C++ Source or Header  |  1998-11-10  |  2KB  |  132 lines

  1. /*
  2. **  Environment functions. 
  3. **
  4. **  getenv, putenv, setenv, unsetenv.
  5. **
  6. **  Replace the SAS/C implementations of these functions by shorter,
  7. **  better ones. (IMHO)
  8. **
  9. **  June 12, 1996: fixed zero-length variables
  10. **  November 7, 1996: replaced static buffer by buffer on stack
  11. **
  12. **      © Irmen de Jong
  13. **      <irmen@bigfoot.com>
  14. */
  15.  
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <errno.h>
  19. #include <proto/dos.h>
  20. #include <dos/dos.h>
  21.  
  22. #define BUFFER_SIZE 256
  23.  
  24. static const char *fixEqualEnd(const char *str, char *buf)
  25. {
  26.     LONG len=strlen(str);
  27.  
  28.     if(str[len-1]=='=')
  29.     {
  30.         strcpy(buf,str);
  31.         buf[len-1]=0; /* unix compatibility */
  32.         return buf;
  33.     }
  34.     return str;
  35. }
  36.  
  37. static const char *fixEqualStart(const char *str)
  38. {
  39.     if(str[0]=='=') return str+1;   /* unix compatibility */
  40.     return str;
  41. }
  42.  
  43. char *getenv(const char *var)
  44. {
  45.     char *val;
  46.     LONG len;
  47.     char buf[BUFFER_SIZE];
  48.  
  49.     var=fixEqualEnd(var,buf);
  50.  
  51.     len=GetVar(var,buf,200,GVF_GLOBAL_ONLY);        /* ENV: ONLY!! */
  52.     if(len>=0)
  53.     {
  54.         if(val=malloc(len+1))
  55.         {
  56.             strcpy(val,buf);
  57.             return val;
  58.         }
  59.         else errno=ENOMEM;
  60.     }
  61.  
  62.     return NULL;
  63. }
  64.  
  65. #ifdef getenv
  66. #undef getenv
  67. char *getenv(const char* var) { return __getenv(var); } /* other getenv */
  68. #endif
  69.  
  70.  
  71. int setenv(const char *name, const char *value, int overwrite)
  72. {
  73.     char lbuf[20];
  74.     char buf[BUFFER_SIZE];
  75.  
  76.     name=fixEqualEnd(name,buf);
  77.     value=fixEqualStart(value);
  78.  
  79.     if(!name || !value)
  80.     {
  81.         errno=EINVAL;
  82.         return -1;
  83.     }
  84.  
  85.     if(overwrite)
  86.     {
  87.         /* Delete global instance first */
  88.         DeleteVar(name,GVF_GLOBAL_ONLY);
  89.         if(SetVar(name,value,-1,GVF_GLOBAL_ONLY)) return 0;
  90.         errno=ENOMEM;
  91.         return -1;
  92.     }
  93.     
  94.     /* Don't overwrite if var is already set */
  95.     if(GetVar(name,lbuf,20,GVF_GLOBAL_ONLY)>=0) return 0;
  96.  
  97.     /* Not set, do it now */
  98.     if(SetVar(name,value,-1,GVF_GLOBAL_ONLY)) return 0;
  99.     errno=ENOMEM;
  100.     return -1;
  101. }
  102.  
  103. /* returns 0 if OK, nonzero if fail */
  104. int putenv(const char *string)
  105. {
  106.     char buf[BUFFER_SIZE];
  107.     char *value;
  108.     if(strlen(string)>=BUFFER_SIZE)
  109.     {
  110.         errno=ENOMEM;
  111.         return -1;
  112.     }
  113.  
  114.     strcpy(buf,string);
  115.     if(value = strchr(buf,'='))
  116.     {
  117.         *value=0;
  118.         return setenv(buf,value+1,1);
  119.     }
  120.     errno=EINVAL;
  121.     return -1;
  122. }
  123.  
  124. /* silently remove environment var name */
  125. void unsetenv(const char *name)
  126. {
  127.     char buf[BUFFER_SIZE];
  128.     name=fixEqualEnd(name,buf);
  129.     DeleteVar(name,GVF_GLOBAL_ONLY);        /* ENV: ONLY! */
  130. }
  131.  
  132.